package com.bbx.taxi.client.DB.Util;

import com.bbx.taxi.client.MyApplication;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * 
 * @author yanxuwen
 */
public class DBHelper extends SQLiteOpenHelper implements DBcolumns {

	private static final String DB_NAME = "bbxpc";
	private static final int DB_VERSION = 25;
	public static DBHelper mInstance;
    private static final String DROP_TABLE = "drop table if exists %s";

	/**
	 * 地址
	 */
	String sql_address = "CREATE TABLE " + TABLE_ADDRESS + " ("
			+ ADDRESS_ID + " INTEGER primary key autoincrement, " + " "
			+ ADDRESS_NAME + " text," + ADDRESS_ADDRESS + " text,"
			+ ADDRESS_LONGITUDE + " text," + ADDRESS_LATITUDE + " text,"+ ADDRESS_CITY + " text,"
			+ ADDRESS_COLLECTION + " text)";
	/**
	 * 历史联系人
	 */
	String sql_contacts = "CREATE TABLE " + TABLE_CONTACTS + " ("
			+ CONTACTS_ID + " INTEGER primary key autoincrement, " + " "
			+ CONTACTS_NAME + " text," + " " + CONTACTS_COLLECTION
			+ " text," + CONTACTS_TEL + " text)";
	/**
	 * 消息
	 */
	String sql_offline = "CREATE TABLE " + TABLE_MESSAGE + " ("
			+ MESSAGE_ID + " INTEGER primary key autoincrement, "
			+ " " + MESSAGE_ORDER_ID + " text," + MESSAGE_TIME + " text," + " "
			+ MESSAGE_TYPE + " INTEGER,"  + MESSAGE_READ + " text,"+ MESSAGE_JSON + " text)";
	/**
	 * 优惠劵
	 */
	String sql_coupon = "CREATE TABLE " + TABLE_COUPON + " (" + COUPON_id
			+ " INTEGER primary key autoincrement, " + " " + COUPON_ID
			+ " text," + COUPON_SUM + " text," + " " + COUPON_EXPIRE_TIME
			+ " text," + COUPON_USABLETIME + " text," + COUPON_USABLETYPE
			+ " text," + COUPON_USABLEORDER + " text," + COUPON_USABLELINE
			+ " text)";
	/**
	 * 推荐
	 */
	String sql_recomen = "CREATE TABLE " + TABLE_RECOMMEN + " (" + RECOMMEN_id
			+ " INTEGER primary key autoincrement, " + " " + RECOMMEN_PHONE
			+ " text," + RECOMMEN_NANE + " text," + RECOMMEN_STATE + " text," +  RECOMMEN_HEADIMG
			+ " text)";
	/**
	 * 我的订单
	 */
	String sql_myorder = "CREATE TABLE " + TABLE_MYORDER + " ("
			+ MYORDER_id + " INTEGER primary key autoincrement, " + " "
			+ MYORDER_ID + " text,"
			+ MYORDER_ORDER_ID + " text," + " "
			+ MYORDER_PASSENGER_ID + " text,"
			+ MYORDER_TC_ID + " text," 
			+ MYORDER_TC_NAME + " text,"
			+ MYORDER_ROUND_TRIP + " text,"
			+ MYORDER_VEHICLE_ID + " text," 
			+ MYORDER_CAR_SEATS + " text,"
			+ MYORDER_ORDER_TYPE + " text,"
			+ MYORDER_ORDER_TIME + " text,"
			+ MYORDER_XIADAN_TIME + " text,"
			+ MYORDER_LINE_ID + " text,"
			+ MYORDER_TRAVEL_TYPE + " text,"
			+ MYORDER_COUPON_ID + " text,"
			+ MYORDER_ORDER_STATUS + " text,"
			+ MYORDER_AUDIT_STATUS + " text,"
			+ MYORDER_VALUATE_STATUS + " text,"
			+ MYORDER_DRIVER_ID + " text,"
			+ MYORDER_DRIVER_NAME + " text,"
			+ MYORDER_CAR_NO + " text,"
			+ MYORDER_DRIVER_PHONE + " text,"
			+ MYORDER_DRIVER_STAR + " text,"
			+ MYORDER_DRIVER_MESSAGE + " text,"
			+ MYORDER_PRICE + " text,"
			+ MYORDER_TOTALPRICE + " text,"
			+ MYORDER_ACTUALPRICE + " text,"
			+ MYORDER_COUPONSUM + " text,"
			+ MYORDER_SUGGEST + " text,"
			+ MYORDER_PURPOSE + " text,"
			+ MYORDER_REJECT_REASON + " text,"
			+ MYORDER_PAIDAN_VTYPE + " text,"
			+ MYORDER_OWN + " text,"
			+ MYORDER_PAY_STATUS + " text,"
			+ MYORDER_VTYPE + " text,"
			+ MYORDER_APPOINT_TYPE + " text)";
	/**
	 * 我的进行中订单
	 */
	String sql_myordering = "CREATE TABLE " + TABLE_MYORDERING + " ("
			+ MYORDERING_id + " INTEGER primary key autoincrement, " + " "
			+ MYORDERING_ID + " text,"
			+ MYORDERING_ORDER_ID + " text," + " "
			+ MYORDERING_PASSENGER_ID + " text,"
			+ MYORDERING_TC_ID + " text," 
			+ MYORDERING_TC_NAME + " text,"
			+ MYORDERING_ROUND_TRIP + " text,"
			+ MYORDERING_VEHICLE_ID + " text," 
			+ MYORDERING_CAR_SEATS + " text,"
			+ MYORDERING_ORDER_TYPE + " text,"
			+ MYORDERING_ORDER_TIME + " text,"
			+ MYORDERING_XIADAN_TIME + " text,"
			+ MYORDERING_LINE_ID + " text,"
			+ MYORDERING_TRAVEL_TYPE + " text,"
			+ MYORDERING_COUPON_ID + " text,"
			+ MYORDERING_ORDER_STATUS + " INTEGER,"
			+ MYORDERING_AUDIT_STATUS + " INTEGER,"
			+ MYORDERING_VALUATE_STATUS + " text,"
			+ MYORDERING_DRIVER_ID + " text,"
			+ MYORDERING_DRIVER_NAME + " text,"
			+ MYORDERING_CAR_NO + " text,"
			+ MYORDERING_DRIVER_PHONE + " text,"
			+ MYORDERING_DRIVER_STAR + " text,"
			+ MYORDERING_DRIVER_MESSAGE + " text,"
			+ MYORDERING_PRICE + " text,"
			+ MYORDERING_TOTALPRICE + " text,"
			+ MYORDERING_ACTUALPRICE + " text,"
			+ MYORDERING_COUPONSUM + " text,"
			+ MYORDERING_SUGGEST + " text,"
			+ MYORDERING_PURPOSE + " text,"
			+ MYORDERING_REJECT_REASON + " text,"
			+ MYORDERING_PAIDAN_VTYPE + " text,"
			+ MYORDERING_OWN + " text,"
			+ MYORDERING_PAY_STATUS + " text,"
			+ MYORDERING_VTYPE + " text,"
			+ MYORDERING_APPOINT_TYPE + " text)";
	/**
	 * 我的订单的locations表
	 */
	String sql_myorder_locations = "CREATE TABLE "
			+ TABLE_MYORDER_LOCATIONS + " (" + MYORDER_LOCATIONS_id
			+ " INTEGER primary key autoincrement, " + " "
			+ MYORDER_LOCATIONS_ORDER_ID + " text," + " "
			+ MYORDER_LOCATIONS_START_ADDRESS + " text,"
			+ MYORDER_LOCATIONS_START_LNG + " text,"
			+ MYORDER_LOCATIONS_START_LAT + " text,"
			+ MYORDER_LOCATIONS_START_NAME + " text,"
			+ MYORDER_LOCATIONS_START_PHONE + " text,"
			+ MYORDER_LOCATIONS_END_ADDRESS + " text,"
			+ MYORDER_LOCATIONS_END_LNG + " text,"
			+ MYORDER_LOCATIONS_END_LAT + " text,"
			+ MYORDER_LOCATIONS_END_NAME + " text,"
			+ MYORDER_LOCATIONS_END_PHONE + " text,"
			+ MYORDER_LOCATIONS_VOLUME + " INTEGER,"
			+ MYORDER_LOCATIONS_WEIGHT + " INTEGER,"
				+ MYORDER_LOCATIONS_COUNT + " INTEGER,"
					+ MYORDER_MUTIPLE_ONCAR + " INTEGER,"
						+ MYORDER_START_NEED_SMS + " INTEGER,"
			+ MYORDER_END_NEED_SMS + " INTEGER)";
	/**订单取消*/
	String sql_order_cencel = "CREATE TABLE "
			+ TABLE_ORDER_CANCEL + " (" + CENCEL_ID
			+ " INTEGER primary key autoincrement, " + " "
			+ CENCEL_ORDER_ID + " text," + " "
			+ CANCEL_REASON + " text)";
	/**线路*/
	String sql_line = "CREATE TABLE "
			+ TABLE_LINE + " (" + LINE_ID
			+ " INTEGER primary key autoincrement, " + " "
			+ LINE_NAME + " text," + " "
			+ LINE_CN_NAME + " text," + " "
			+ LINE_JSON + " text)";
	/**市内线路*/
	String sql_linesn = "CREATE TABLE "
			+ TABLE_LINESN + " (" + LINESN_ID
			+ " INTEGER primary key autoincrement, " + " "
			+ LINESN_NAME + " text," + " "
			+ LINESN_CN_NAME + " text," + " "
			+ LINESN_JSON + " text)";
	/**
	 * 未审核订单
	 */
	String sql_unprocessed= "CREATE TABLE " + TABLE_UNPROCESSED+ " ("
			+ UNPROCESSED_id + " INTEGER primary key autoincrement, " + " "
			+ UNPROCESSED_ORDER_ID + " text," 
			+ UNPROCESSED_PASSENGER_ID + " text,"
			+ UNPROCESSED_PURPOSE + " text," 
			+ UNPROCESSED_ROUND_TRIP + " text,"
			+ UNPROCESSED_VEHICLE_ID + " text," 
			+ UNPROCESSED_DRIVER_ID + " text,"
			+ UNPROCESSED_DRIVER_NAME + " text,"
			+ UNPROCESSED_PRICE + " text,"
			+ UNPROCESSED_APPOINT_TIME + " text)";
	/**
	 * 审核记录
	 */
	String sql_unprocessed_record= "CREATE TABLE " + TABLE_UNPROCESSED_RECORD+ " ("
			+ UNPROCESSED_RECORD_id + " INTEGER primary key autoincrement, " + " "
			+ UNPROCESSED_RECORD_ORDER_ID + " text," 
			+ UNPROCESSED_RECORD_PASSENGER_ID + " text,"
			+ UNPROCESSED_RECORD_PURPOSE + " text," 
			+ UNPROCESSED_RECORD_ROUND_TRIP + " text,"
			+ UNPROCESSED_RECORD_VEHICLE_ID + " text," 
			+ UNPROCESSED_RECORD_DRIVER_ID + " text,"
			+ UNPROCESSED_RECORD_DRIVER_NAME + " text,"
			+ UNPROCESSED_RECORD_PRICE + " text,"
			+ UNPROCESSED_RECORD_PASS_STATUS + " text,"
			+ UNPROCESSED_RECORD_APPOINT_TIME + " text)";
	/**推荐事由*/
	String sql_tag_message= "CREATE TABLE " + TABLE_TAGMSG+ " ("
			+ TAG_ID + " INTEGER primary key autoincrement, " + " "
			+ TAG_NAME + " text)";  
	public DBHelper(Context context,String uid) {
		super(context, DB_NAME+"_"+uid, null, DB_VERSION);
	}
	public DBHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}
	public DBHelper(Context context, int version,String uid) {
		super(context, uid+"_"+DB_NAME, null, version);
	}

	public synchronized static DBHelper getInstance(Context context,String uid) {
		mInstance=null;
		if (mInstance == null) {
			if(uid!=null&&!uid.equals("")){
				mInstance = new DBHelper(context,DB_VERSION,uid);
			}
			else{
				mInstance = new DBHelper(context);
			}
			
		}
		return mInstance;
	};

	@Override
	public void onCreate(SQLiteDatabase db) {

		
		db.execSQL(sql_address);// 地址
		db.execSQL(sql_contacts);// 常用联系人
		db.execSQL(sql_offline);// 离线消息
		db.execSQL(sql_coupon);// 优惠劵
		db.execSQL(sql_recomen);// 推荐
		db.execSQL(sql_myorder);// 我的订单
		db.execSQL(sql_myordering);// 我的进行中订单的
		db.execSQL(sql_myorder_locations);// 我的订单的locations表
		db.execSQL(sql_order_cencel);//取消订单
		db.execSQL(sql_line);//线路
		db.execSQL(sql_linesn);//市内线路
		db.execSQL(sql_unprocessed);//未审核订单
		db.execSQL(sql_unprocessed_record);//审核记录
		db.execSQL(sql_tag_message);//推荐事由
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.e("",oldVersion+",,,"+newVersion);
         if(oldVersion<DB_VERSION){
        	 String DROP_TABLE_TABLE_MYORDER = String.format(DROP_TABLE, TABLE_MYORDER);
        	 String DROP_TABLE_TABLE_MYORDERING = String.format(DROP_TABLE, TABLE_MYORDERING);
        	 db.execSQL(DROP_TABLE_TABLE_MYORDER);
        	 db.execSQL(DROP_TABLE_TABLE_MYORDERING);
        	 Log.e("","升级数据库");
     		db.execSQL(sql_myorder);
     		db.execSQL(sql_myordering);
     		db.execSQL(sql_tag_message);//推荐事由
         } 
	}

}
